/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomerAccountView.java
 *
 * Created on Apr 3, 2010, 11:36:00 PM
 */

package ticketmaster.presentation.customer;

import java.awt.Color;
import java.sql.*;
import java.util.*;
import javax.swing.JOptionPane;
import ticketmaster.accounting.*;
import ticketmaster.businesslogic.OrderController;
import ticketmaster.businessobject.LoginUser;
import ticketmaster.businessobject.PurchaseType;
import ticketmaster.common.*;
import ticketmaster.dataobject.*;
import ticketmaster.dao.*;
import ticketmaster.presentation.customer.purchase.PaymentReviewView;
import ticketmaster.util.*;

/**
 *
 * @author Xiaolong Xue
 */
public class CustomerAccountView extends CustomerBasePanel
{

    static final double YEARLYFEE = 5.0;
    /** Creates new form CustomerAccountView */
    public CustomerAccountView()
    {
        super();
        initComponents();
        this.setBackground(Color.white);
        getBankcardBalance();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        btnUpgrade = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtBankcardBalance = new javax.swing.JTextField();

        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setName("Form"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ticketmaster.TicketMasterApp.class).getContext().getResourceMap(CustomerAccountView.class);
        jPanel3.setBackground(resourceMap.getColor("jPanel3.background")); // NOI18N
        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel4.setBackground(resourceMap.getColor("jPanel4.background")); // NOI18N
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(resourceMap.getColor("jPanel4.border.border.lineColor"), 1, true), resourceMap.getString("jPanel4.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel4.border.titleFont"))); // NOI18N
        jPanel4.setName("jPanel4"); // NOI18N
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnUpgrade.setFont(resourceMap.getFont("btnUpgrade.font")); // NOI18N
        btnUpgrade.setText(resourceMap.getString("btnUpgrade.text")); // NOI18N
        btnUpgrade.setName("btnUpgrade"); // NOI18N
        btnUpgrade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpgradeActionPerformed(evt);
            }
        });
        jPanel4.add(btnUpgrade, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 110, -1, 54));

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel4.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 60, 195, 30));

        jPanel3.add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 210, 540, 210));

        jPanel5.setBackground(resourceMap.getColor("jPanel5.background")); // NOI18N
        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(resourceMap.getColor("jPanel5.border.border.lineColor"), 1, true), resourceMap.getString("jPanel5.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel5.border.titleFont"))); // NOI18N
        jPanel5.setName("jPanel5"); // NOI18N
        jPanel5.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel5.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 60, -1, 30));

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel5.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 60, -1, 30));

        txtBankcardBalance.setBackground(resourceMap.getColor("txtBankcardBalance.background")); // NOI18N
        txtBankcardBalance.setEditable(false);
        txtBankcardBalance.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        txtBankcardBalance.setText(resourceMap.getString("txtBankcardBalance.text")); // NOI18N
        txtBankcardBalance.setName("txtBankcardBalance"); // NOI18N
        jPanel5.add(txtBankcardBalance, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 60, 169, 30));

        jPanel3.add(jPanel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, 540, 160));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(385, 385, 385)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(73, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void getBankcardBalance()
    {
        int userID = LoginUser.getUser().getUserID();
        Bankcard bankcard = new Bankcard();
        bankcard.setCustomerID(userID);
        DAOFactory.getInstance().getBankcardDAO().findBankcard(bankcard);
        txtBankcardBalance.setText(Double.toString(bankcard.getBalance()));
    }
    private void btnUpgradeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpgradeActionPerformed
        // TODO add your handling code here:
        payMemberFee();
    }//GEN-LAST:event_btnUpgradeActionPerformed

    private void payMemberFee()
    {
        Order order = new Order();
        order.setCustomerID(LoginUser.getUser().getUserID());
        order.setOrderDate(DateTimeHelper.getCurrentDateTime());
        order.setOrderType(OrderType.YearlyFee.ordinal());
        order.setCost(YEARLYFEE + TaxManager.calculateTax(YEARLYFEE));
        order.setShippingMethodID(2);
        Event yearlyFeeEvent = new Event();
        yearlyFeeEvent.setName("Yearly Fee");
        yearlyFeeEvent.setCancelled(true);

        DAOFactory.getInstance().getEventDAO().getEventsDO(yearlyFeeEvent);
        order.setEventID(yearlyFeeEvent.getEventID());

        OrderController controller = new OrderController();
        order.setOrderID(controller.createOrder(order));
        if (order.getOrderID() > 0)
        {
            PaymentReviewView prv = new PaymentReviewView(YEARLYFEE, TaxManager.calculateTax(YEARLYFEE));
            prv.setOrder(order);
            PurchaseType.setPurchaseType(PurchaseTypes.Upgrade);
            prv.setBookedTickets(this.getBookedTickets());
            prv.setPnlParent(this.getPnlParent());
            this.getPnlParent().removeAll();
            this.getPnlParent().add(prv);
            this.getPnlParent().validate();
            this.getPnlParent().repaint();
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnUpgrade;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JTextField txtBankcardBalance;
    // End of variables declaration//GEN-END:variables

}
